package com.cvte.boe.media;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapRegionDecoder;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.view.KeyEvent;
import android.view.Menu;
import android.widget.ImageView;

import com.cvte.boe.media.dao.MediaSettingDao;
import com.cvte.boe.media.dao.MediaSettingDaoImpl;
import com.cvte.boe.media.model.LocalDeviceInfo;
import com.cvte.boe.media.model.LocalMediaInfo;
import com.cvte.boe.media.player.ImagePlayer;
import com.cvte.boe.media.utils.LogUtil;
import com.cvte.boe.media.utils.PlayerUtil;
import com.cvte.boe.media.utils.SettingUtil;
import com.cvte.boe.media.utils.StringUtils;
import com.cvte.boe.media.views.AutoHideLayout;
import com.cvte.boe.media.views.GlobalDialog;
import com.cvte.boe.media.views.settingpage.PageImagePlayerSettings;
import com.cvte.boe.media.views.widget.MarqueeTextView;

import java.io.File;

import pl.droidsonroids.gif.GifImageView;

/**
 * Created by wuguitong on 2018/4/18.
 */
public class ImagePlayActivity extends Activity implements PageImagePlayerSettings.OnImageSettingListener{
    private AutoHideLayout ahTopBar;
    private AutoHideLayout ahBottomBar;
    private Context mContext;
    private String mCurrentPath;
    private Uri intentUri;
    private ImageView mCurrentImage;
    private GifImageView mCurGifImageView;
    private ImagePlayer mImagePlayer;
    private MarqueeTextView mTVName;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext = this;
        setContentView(R.layout.activity_image_player);
        Intent intent = getIntent();
        String action = intent.getAction();
        if (Intent.ACTION_VIEW.equals(action)) {
            intentUri = intent.getData();
            if(intentUri != null){
                mCurrentPath = new File(intentUri.getPath()).getParent();
            }
        }
        ahTopBar = (AutoHideLayout)findViewById(R.id.ah_image_player_top);
        ahBottomBar = (AutoHideLayout)findViewById(R.id.ah_image_player_bottom);
        mTVName = (MarqueeTextView)findViewById(R.id.tv_image_name);
        mTVName.setMarqueeState(true);
        MediaApplication.registerMountActionListener(mountActionListener);
        mCurrentImage = (ImageView)findViewById(R.id.iv_current_image);
        mCurGifImageView = (GifImageView)findViewById(R.id.gif_current_view);
        MediaSettingDao dao = new MediaSettingDaoImpl(mContext);
        mImagePlayer = new ImagePlayer();
        mImagePlayer.initPlayer(mContext,mCurrentImage,mCurGifImageView,mTVName,ahTopBar,ahBottomBar,mCurrentPath,intentUri.getPath(),dao.getImageLoop(),dao.getImageLoopTimer());
    }

    @Override
    protected void onStop() {
        GlobalDialog.destroyInstance();
        super.onStop();
    }
    @Override
    protected void onDestroy() {
        mImagePlayer.deinitPlayer();
        MediaApplication.unRegisterMountActionListener(mountActionListener);
        super.onDestroy();
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode){
            case KeyEvent.KEYCODE_MENU:
                PageImagePlayerSettings pageImagePlayerSettings = new PageImagePlayerSettings(mContext);
                pageImagePlayerSettings.setOnImagePlayerListener(this);
                GlobalDialog.getInstance(this).setContentView(pageImagePlayerSettings);
                GlobalDialog.getInstance(this).show();
                return true;
            case KeyEvent.KEYCODE_BACK:
                this.finish();
                return true;
            case KeyEvent.KEYCODE_DPAD_LEFT:
                mImagePlayer.showPreImage();
                mImagePlayer.showBar();
                break;
            case KeyEvent.KEYCODE_DPAD_RIGHT:
                mImagePlayer.showNextImage();
                mImagePlayer.showBar();
                break;
            case KeyEvent.KEYCODE_DPAD_CENTER:
                mImagePlayer.showBar();
                break;
            default:
                break;
        }
        return false;
    }

    @Override
    public void onUpdateImageLoop(SettingUtil.ILOOP loop) {
        mImagePlayer.setImageLoop(loop);
    }

    @Override
    public void onUpdateImageLoopTimer(SettingUtil.ILOOPTIMER timer) {
        mImagePlayer.setImageLoopTimer(timer);
    }

    MediaApplication.MountActionListener mountActionListener = new MediaApplication.MountActionListener() {
        @Override
        public void onReceive(String volumePath, boolean mount) {
            if (StringUtils.formatMountPointWithSeparator(mCurrentPath).startsWith(StringUtils.formatMountPointWithSeparator(volumePath))) {
                if (!mount) {
                    ImagePlayActivity.this.finish();
                }
            }
        }
    };
}
